<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/30
 * Time: 15:16
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goodsattr extends Base
{
    /**
     * 产品属性首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $attr_status = input('attr_status');
            $where['attr_status'] = ['BETWEEN', [1, 2]];

            if($attr_status) $where['attr_status'] = $attr_status;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods_attr')->where($where)->count();// 获取总条数
            $lists = Db::table('up_goods_attr')
                ->where($where)
                ->field('attr_id, attr_name_en, attr_name_es, attr_name_tp, attr_status, attr_sort, attr_time')
                ->page($Nowpage, $limits)
                ->order('attr_sort desc, attr_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 添加属性
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function attrAdd(){
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $attr_status = input('attr_status');
            $attr_sort = input('attr_sort');

            $has = Db::table('up_goods_attr')->where(['attr_name_en' => $name_en])->find();
            if($has) return json(['code' => 100, 'msg' => '该属性已存在']);

            $insert = [
                'attr_name_en' => $name_en,
                'attr_name_es' => $name_es,
                'attr_name_tp' => $name_tp,
                'attr_status' => $attr_status,
                'attr_sort' => $attr_sort,
                'attr_time' => initTime()
            ];
            $res = Db::table('up_goods_attr')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑属性
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function attrEdit(){
        $attr_id = input('attr_id');
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $attr_status = input('attr_status');
            $attr_sort = input('attr_sort');

            $has = Db::table('up_goods_attr')
                ->where(['attr_name_en' => $name_en])
                ->find();
            if($has && ($attr_id != $has['attr_id'])) return json(['code' => 100, 'msg' => '该属性已存在']);

            $update = [
                'attr_name_en' => $name_en,
                'attr_name_es' => $name_es,
                'attr_name_tp' => $name_tp,
                'attr_status' => $attr_status,
                'attr_sort' => $attr_sort,
                'attr_update_time' => initTime()
            ];
            $res = Db::table('up_goods_attr')->where('attr_id', $attr_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_goods_attr')->where('attr_id', $attr_id)->field('attr_id, attr_name_en, attr_name_es, attr_name_tp, attr_status, attr_sort')->find();
        return $this->fetch('', ['data' => $data]);
    }

    /**
     * 上架/下架
     * @return \think\response\Json
     */
    public function goodsAttrChangeStatus(){
        $attr_id = input('id');
        $status = Db::table('up_goods_attr')->where('attr_id', $attr_id)->value('attr_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_goods_attr')->where('attr_id', $attr_id)->setField('attr_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除属性
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function goodsAttrDel(){
        $attr_id = input('id');
        $res = Db::table('up_goods_attr')->where('attr_id', $attr_id)->setField('attr_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}